package genedit.controller.interfaceActions;

import genedit.controller.AddCommand;
import genedit.gui.GMainFrame;
import genedit.gui.GProjectTree;
import genedit.model.graph.GraphDocument;
import genedit.model.graph.GraphElement;
import genedit.model.graph.SubdocumentElement;
import genedit.view.graph.GraphDocumentView;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;

/**
 * Akcija zadužena za kreiranje poddijagrama u izabranom dijagramu.
 * 
 * @author Voja
 * @version 1.1 5/1/14
 */
public class GNewSubDocumentA extends AbstractAction {
	private static final long serialVersionUID = 1L;

	protected static GNewSubDocumentA newD = null;

	protected GNewSubDocumentA() {
		putValue(NAME, "New Subdocument");
		putValue(SHORT_DESCRIPTION, "New Subdocument");
		putValue(SMALL_ICON, new ImageIcon("resources/icons/setNew.png"));
		putValue(MNEMONIC_KEY, KeyEvent.VK_M);
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control M"));
	}

	public static AbstractAction getInstance() {
		if (newD == null)
			newD = new GNewSubDocumentA();
		return newD;
	}

	/**
	 * Akcija kreira novi poddijagram u selektovanom dijagramu.
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		Object p = GProjectTree.getInstance().getTree().getLastSelectedPathComponent();
		if (!(p instanceof GraphDocument))
			return;

		GraphDocument parentDocument = (GraphDocument) p;
		GraphDocumentView parentDocumentView = GMainFrame.getInstance().getGraphDocumentView(
				parentDocument);
		if (parentDocumentView == null)
			return;

		GraphDocument newDocument = new GraphDocument(parentDocument, GMainFrame.getInstance()
				.getResourceBundle().getString("Subdiagram"));
		parentDocument.addSubDocument(newDocument);
		GMainFrame.getInstance().addGraphDocumentView(newDocument);

		GraphElement newElement = new SubdocumentElement(parentDocument,
				new Point2D.Double(30, 20), new Dimension(60, 40), "Subdiagram", newDocument);

		AddCommand addCommand = new AddCommand(false);
		addCommand.getAddedElements().add(newElement);
		addCommand.setDocument(parentDocumentView);
		parentDocumentView.getModel().getInvoker().executeCommand(addCommand);
	}
}
